package com.spps;

import java.util.HashMap;

public class Config {
    HashMap<String, String> element = new HashMap<>();

    Config(String[] args) {

        // 实例化命令终端控制对象
        HashMap<String, String> optionMap = new HashMap<>();
        optionMap.put("-s", "sourcePath");
        optionMap.put("-c", "className");
        optionMap.put("-o", "outputPath");
        optionMap.put("-t", "tableName");
        optionMap.put("-a", "apiName");
        optionMap.put("-v", "viewName");
        optionMap.put("-e", "extension");
        optionMap.put("--module", "moduleName");
        optionMap.put("--model", "modelName");
        optionMap.put("--controller", "controllerName");
        optionMap.put("--api", "apiName");
        optionMap.put("--view", "viewName");
        Commander commander = new Commander(args, optionMap);

        // 默认值
        element.put("action", commander.get("action"));
        element.put("sourcePath", "");
        element.put("className", "");
        element.put("outputPath", "");
        element.put("apiName", "");
        element.put("viewName", "");
        element.put("extension", "");
        element.put("modelName", "");
        element.put("moduleName", "");
        element.put("modelOutputPath", "");
        element.put("databaseName", "");
        element.put("prefix", "");
        element.put("tableName", "");
        element.put("user", "");
        element.put("password", "");
        element.put("sourceDirectory", "");
        element.put("modelOutputDirectory", "");
        element.put("controllerOutputDirectory", "");
        element.put("apiOutputDirectory", "");
        element.put("controllerName", "");

        // 实例化配置文件对象
        try {
            String packagePath = PathFactory.getPackagePath(); // 当前包的路径
            String configPath = packagePath + "config.xml"; // 配置文件
            Configuration configuration = new Configuration(configPath);

            element.forEach((key, value) -> {
                if (null != configuration.getValue(key) && 0 != configuration.getValue(key).length()) {
                    element.put(key, configuration.getValue(key));
                }
                if (null != commander.get(key) && 0 != commander.get(key).length()) {
                    element.put(key, commander.get(key));
                }
            });
        } catch (Exception e) {
        }
    }

    String get(String key) {
        return element.get(key);
    }
}
